/*
 * @Description: Dom操作类
 * @Author: seadon
 * @LastEditors: seadon
 * @Date: 2021-03-24 11:16:46
 * @LastEditTime: 2021-04-25 10:00:10
 */
/* istanbul ignore next */
export function on(
	element: Element | HTMLElement | Document | Window,
	event: string,
	handler: EventListenerOrEventListenerObject
): void {
	if (element && event && handler) {
		element.addEventListener(event, handler, false)
	}
}

/* istanbul ignore next */
export function off(
	element: Element | HTMLElement | Document | Window,
	event: string,
	handler: Fn
): void {
	if (element && event && handler) {
		element.removeEventListener(event, handler, false)
	}
}

// native parentsUntil
export function parentsUntil(
	el: HTMLElement | any,
	selector: string,
	filter: string
) {
	const result = []
	const matchesSelector =
		el.matches ||
		el.webkitMatchesSelector ||
		el.mozMatchesSelector ||
		el.msMatchesSelector

	// match start from parent
	el = el.parentElement
	while (el && !matchesSelector.call(el, selector)) {
		if (!filter) {
			result.push(el)
		} else {
			if (matchesSelector.call(el, filter)) {
				result.push(el)
			}
		}
		el = el.parentElement
	}
	return result
}
